package yja.gzw.app.activity

import android.content.Intent
import android.net.Uri
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import com.bytedance.sdk.open.aweme.base.MediaContent
import com.bytedance.sdk.open.aweme.base.VideoObject
import com.bytedance.sdk.open.aweme.common.constants.ParamKeyConstants
import com.bytedance.sdk.open.aweme.share.Share
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory
import com.google.android.material.tabs.TabLayout
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityWodezhanghuasBinding
import yja.gzw.app.fragmentview.PinataiquanbuFragment
import yja.gzw.app.fragmentview.PingtaibiliFragment
import yja.gzw.app.fragmentview.PingtaixiguanFragment
import yja.gzw.app.fragmentview.PintaidouyingFragment
import yja.gzw.app.fragmentview.pingtaikuaishouFragment
import yja.gzw.app.viewmodel.Mainviewmodel
import java.io.File


class WodezhanghuasActivity :BaseActivity<Mainviewmodel, ActivityWodezhanghuasBinding>(R.layout.activity_wodezhanghuas){

    val tuut1= mutableListOf<Int>(R.mipmap.quanbu,R.mipmap.douying,R.mipmap.kuaishou,R.mipmap.bilibliti,R.mipmap.xiguanships)

    val userDatalist= mutableListOf<String>("全部","抖音","快手","bili","西瓜视频")
    val list_fragment= mutableListOf<Fragment>(
        PinataiquanbuFragment(),
        PintaidouyingFragment(),
        pingtaikuaishouFragment(), PingtaibiliFragment(), PingtaixiguanFragment()
    )
    override fun initView() {
        val request = Share.Request()
       // request.mState = "xxx"
        val mUri = ArrayList<String>()
        val file = File("/sdcard/Download/downloaded_video.mp4")
        val uri: Uri = FileProvider.getUriForFile(this@WodezhanghuasActivity,"yja.gzw.app" + ".fileProvider", file)
        this.grantUriPermission(ParamKeyConstants.DOUYIN_PACKAGE_NAME,uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        mUri.add(uri.toString());
        val videoObject = VideoObject()
        videoObject.mVideoPaths = mUri
        val videoContent = MediaContent()
        videoContent.mMediaObject = videoObject
        request.mMediaContent = videoContent


        binding.fabu.setOnClickListener {
            val douyinOpenApi = DouYinOpenApiFactory.create(this)
            //如果预期直接拉起发布页，可以判断抖音是否支持。
// 目前直接到发布页仅支持单个视频
            if (douyinOpenApi.isAppSupportShareToPublish) {
                request.shareToPublish = true
            }

// 调起分享
            douyinOpenApi.share(request)

        }


        binding.bangdingzhanghao.setOnClickListener {
           val intent=Intent(this,BasedingzhanghaoActivity::class.java)
            startActivity(intent)
        }

        binding.viewpager.adapter=object : FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {

                return list_fragment.size

            }

            override fun getItem(position: Int): Fragment {

                return list_fragment[position]

            }

            override fun getPageTitle(position: Int): CharSequence {
                return userDatalist[position]
            }

        }
        binding.tabs.setupWithViewPager(binding.viewpager)

        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                tab.setIcon(tuut1[tab.position])

            }

            override fun onTabUnselected(tab: TabLayout.Tab) {

                tab.setIcon(tuut1[tab.position])
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }




        })

        for (i in 0  until tuut1.size ){

            binding.tabs.getTabAt(i)?.setIcon(tuut1[i])



        }
    }

    override fun dataObservese() {




    }


}